﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TesteOnline.Bll;
using TesteOnline.Model;

namespace TesteOnline
{
    public partial class summaryForm : System.Web.UI.Page
    {

        Wrapper wrapper = new Wrapper();
        Avaliation av;

        protected void Page_Load(object sender, EventArgs e)
        {
            //foreach ( string s in Request.Params)
            //{
            //    Response.Write(s + "</br>");
            //}
            av = wrapper.GetAvaliation();

            var v =
                from s in Request.Params.AllKeys
                where s.StartsWith("q_")
                select s;
            foreach( string k in v)
            {

                //Response.Write(k + "  " + Request.Params[k] + "</br>");
                ProcessAnswer(k, Request.Params[k]);
            }

            Response.Write("-------------------------</br>");
            foreach (int q in av.Questions.Keys)
            {
                String r;
                r = string.Format("{0} {1} </br>", q, av.Questions[q].Result);

                Response.Write(r);
            }
        }

        private void ProcessAnswer(string k, string p)
        {
            
            string [] questionSplit = k.Split('_');
            
            int questionId = Int32.Parse(questionSplit[1]);
            int[] answers = GetAnswers(p);

            string tmp;
            tmp = string.Format("{0} {1} ___", questionId, answers.Length);
            foreach (int i in answers)
            {
                tmp += ";" + i ;
            }
            Response.Write(tmp + "</br>")  ;
            av.Questions[questionId].SetAnswers(answers);



        }

        private int[] GetAnswers(string p)
        {
            string[] optionSplit = p.Split(',');
            List<int> ret = new List<int>();
            foreach (string s in optionSplit)
            {
                ret.Add(Int32.Parse(s));
            }
            return ret.ToArray();
            
        }
    }
}